package com.key.custom.plm.service.serviceImp;

import org.hibernate.Query;
import org.hibernate.Session;

import com.key.base.service.serviceImp.BaseServiceImp;
import com.key.custom.plm.dao.IHrPLMDelegateDao;
import com.key.custom.plm.model.HrPLMDelegate;
import com.key.custom.plm.service.IHrPLMDelegateService;
import com.key.util.HibernateUtil;

public class HrPLMDelegateServiceImp extends BaseServiceImp implements
		IHrPLMDelegateService {
	IHrPLMDelegateDao hrPLMDelegateDao;

	public IHrPLMDelegateDao getHrPLMDelegateDao() {
		return hrPLMDelegateDao;
	}

	public void setHrPLMDelegateDao(IHrPLMDelegateDao hrPLMDelegateDao) {
		this.hrPLMDelegateDao = hrPLMDelegateDao;
	}

	/**
	 * model
	 * 
	 * @param hrPLMDelegate
	 */
	@Override
	public void add(HrPLMDelegate hrPLMDelegate) throws Exception {
		try {
			hrPLMDelegateDao.add(hrPLMDelegate);
		} catch (Exception e) {
			e.printStackTrace();
			throw e;
		}
	}

	@Override
	public void deleteAllObj(String className, String[] ids) throws Exception {
		Session session = HibernateUtil.getHrIntSessionFactory().openSession();
		try {
			StringBuffer queryHQL = new StringBuffer();
			session.beginTransaction();
			for (int i = 0; i < ids.length; i++) {
				String id = ids[i];
				String hql = "delete from " + className + " where id='" + id
						+ "'";
				System.out.println(hql);
				Query query = session.createQuery(hql);
				System.out.println(query.executeUpdate());
			}
			session.getTransaction().commit();
		} catch (Exception e) {
			session.getTransaction().rollback();
			throw e;
		} finally {
			session.flush();
			session.close();
		}
	}

	/**
	 * model
	 * 
	 * @param hrPLMDelegate
	 */
	@Override
	public void update(HrPLMDelegate hrPLMDelegate) throws Exception {
		try {
			hrPLMDelegateDao.update(hrPLMDelegate);
		} catch (Exception e) {
			e.printStackTrace();
			throw e;
		}
	}
}
